package com.lpwa.web.controller.system;

import java.util.List;

import com.lpwa.framework.util.ShiroUtils;
import com.lpwa.system.domain.AlarmQuickText;
import com.lpwa.system.domain.AlarmRuleConfig;
import com.lpwa.system.mapper.TagAlarmDataMapper;
import com.lpwa.system.service.ITagAlarmDataService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.lpwa.common.annotation.Log;
import com.lpwa.common.enums.BusinessType;
import com.lpwa.system.domain.TagAlarmData;
import com.lpwa.common.core.controller.BaseController;
import com.lpwa.common.core.page.TableDataInfo;
import com.lpwa.common.core.domain.AjaxResult;
import com.lpwa.common.utils.poi.ExcelUtil;

/**
 * 标签报警历史 信息操作处理
 * 
 * @author tanmu
 * @date 2019-06-22
 */
@Controller
@RequestMapping("/system/tagAlarmData")
public class TagAlarmDataController extends BaseController
{
    private String prefix = "system/tagAlarmData";
	
	@Autowired
	private ITagAlarmDataService tagAlarmDataService;
	@Autowired
	private TagAlarmDataMapper tagAlarmDataMapper;
	@RequiresPermissions("system:tagAlarmData:view")
	@GetMapping()
	public String tagAlarmData(ModelMap mmap)
	{
		String loginName = ShiroUtils.getLoginName();
		AlarmQuickText alarmQuickText = new AlarmQuickText();
		alarmQuickText.setCreateBy(loginName);
		List<AlarmQuickText> alarmQuickTextList= tagAlarmDataMapper.selectAlarmQuickTextList(alarmQuickText);
		mmap.put("alarmQuickTextList", alarmQuickTextList);
	    return prefix + "/tagAlarmData";
	}
	@GetMapping("/{id}")
	public String tagAlarmDataById(@PathVariable("id") String id ,ModelMap mmap)
	{
		String loginName = ShiroUtils.getLoginName();
		AlarmQuickText alarmQuickText = new AlarmQuickText();
		alarmQuickText.setCreateBy(loginName);
		List<AlarmQuickText> alarmQuickTextList= tagAlarmDataMapper.selectAlarmQuickTextList(alarmQuickText);
		mmap.put("alarmQuickTextList", alarmQuickTextList);
		mmap.put("id", id);
		return prefix + "/tagAlarmData";
	}
	/**
	 * 查询标签报警历史列表
	 */
	@RequiresPermissions("system:tagAlarmData:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(TagAlarmData tagAlarmData)
	{
		startPage();
        List<TagAlarmData> list = tagAlarmDataService.selectTagAlarmDataList(tagAlarmData);
		return getDataTable(list);
	}
	
	
	/**
	 * 导出标签报警历史列表
	 */
	@RequiresPermissions("system:tagAlarmData:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TagAlarmData tagAlarmData)
    {
    	List<TagAlarmData> list = tagAlarmDataService.selectTagAlarmDataList(tagAlarmData);
        ExcelUtil<TagAlarmData> util = new ExcelUtil<TagAlarmData>(TagAlarmData.class);
        return util.exportExcel(list, "tagAlarmData");
    }
	
	/**
	 * 新增标签报警历史
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存标签报警历史
	 */
	@RequiresPermissions("system:tagAlarmData:add")
	@Log(title = "标签报警历史", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(TagAlarmData tagAlarmData)
	{		
		return toAjax(tagAlarmDataService.insertTagAlarmData(tagAlarmData));
	}

	/**
	 * 修改标签报警历史
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") Long id, ModelMap mmap)
	{
		TagAlarmData tagAlarmData = tagAlarmDataService.selectTagAlarmDataById(id);
		mmap.put("tagAlarmData", tagAlarmData);
		String loginName = ShiroUtils.getLoginName();
		AlarmQuickText alarmQuickText = new AlarmQuickText();
		alarmQuickText.setCreateBy(loginName);
		List<AlarmQuickText> alarmQuickTextList= tagAlarmDataMapper.selectAlarmQuickTextList(alarmQuickText);
		mmap.put("alarmQuickTextList", alarmQuickTextList);
	    return prefix + "/edit";
	}

	/**
	 * 修改保存标签报警历史
	 */
	@RequiresPermissions("system:tagAlarmData:edit")
	@Log(title = "标签报警历史", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(TagAlarmData tagAlarmData)
	{
		tagAlarmData.setUpdateBy(ShiroUtils.getLoginName());
		return toAjax(tagAlarmDataService.updateTagAlarmData(tagAlarmData));
	}
	
	/**
	 * 删除标签报警历史
	 */
	@RequiresPermissions("system:tagAlarmData:remove")
	@Log(title = "标签报警历史", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(tagAlarmDataService.deleteTagAlarmDataByIds(ids));
	}
	/**
	 * 修改报警规则配置
	 */
	@GetMapping("/rule")
	public String rule(ModelMap mmap)
	{
		AlarmRuleConfig alarmRuleConfig = tagAlarmDataService.selectAlarmRuleConfigById(1L);
		mmap.put("alarmRuleConfig", alarmRuleConfig);
		return prefix + "/rule";
	}
	/**
	 * 修改报警规则配置
	 */
	@RequiresPermissions("system:tagAlarmData:rule")
	@Log(title = "标签报警历史", businessType = BusinessType.UPDATE)
	@PostMapping("/editRule")
	@ResponseBody
	public AjaxResult editRule(AlarmRuleConfig alarmRuleConfig)
	{
		alarmRuleConfig.setUpdateBy(ShiroUtils.getLoginName());
		return toAjax(tagAlarmDataService.updateAlarmRuleConfig(alarmRuleConfig));
	}
	/**
	 * 修改报警规则配置
	 */
	@GetMapping("/quickText")
	public String quickText(ModelMap mmap)
	{
		String loginName = ShiroUtils.getLoginName();
		AlarmQuickText alarmQuickText = new AlarmQuickText();
		alarmQuickText.setCreateBy(loginName);
		List<AlarmQuickText> alarmQuickTextList= tagAlarmDataMapper.selectAlarmQuickTextList(alarmQuickText);
		mmap.put("alarmQuickTextList", alarmQuickTextList);
		return prefix + "/quickText";
	}
	@RequiresPermissions("system:tagAlarmData:edit")
	@Log(title = "处理快捷文本", businessType = BusinessType.UPDATE)
	@PostMapping("/saveQuickText")
	@ResponseBody
	public AjaxResult saveQuickText(@RequestParam("list") List<String> list)
	{
		String loginName = ShiroUtils.getLoginName();
		//保存处理快捷文本
		return toAjax(tagAlarmDataService.saveQuickText(list,loginName));
	}
}
